package com.len.core.shiro;

import com.alibaba.fastjson.JSONArray;
import com.len.base.CurrentMenu;
import com.len.base.CurrentRole;
import com.len.base.CurrentUser;
import com.len.core.token.PasswordToken;
import com.len.entity.SysMenu;
import com.len.entity.SysRole;
import com.len.entity.SysUser;
import com.len.service.MenuService;
import com.len.service.RoleMenuService;
import com.len.service.RoleUserService;
import com.len.service.SysUserService;
import com.len.util.Md5Util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhuxiaomeng
 * @date 2017/12/4.
 * @email 154040976@qq.com
 */
@Service
public class LoginRealm extends AuthorizingRealm{

  @Autowired
  private SysUserService userService;
 

  //此Realm只支持PasswordToken
  public Class<?> getAuthenticationTokenClass() {
      return PasswordToken.class;
  }
  /**
   * 获取认证 这里只需要认证登录，成功之后派发 json web token 授权在那里进行
   * @param principalCollection
   * @return
   */
  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
   return null;
  }

  /**
   * 获取授权
   * @param authenticationToken
   * @return
   * @throws AuthenticationException
   */
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
      throws AuthenticationException {
      if (!(authenticationToken instanceof PasswordToken)){
          return null;
         }
       PasswordToken  upToken=(PasswordToken)authenticationToken;   
         
      if(null==authenticationToken.getPrincipal()||null==authenticationToken.getCredentials()){
          throw new UnknownAccountException("账户密码不正确");
      }
     String username = (String)authenticationToken.getPrincipal();
     SysUser sysUser = userService.login(username);
     if (sysUser != null) {
         // 用盐对密码进行MD5加密
         //((PasswordToken) authenticationToken).setPassword(Md5Util.getMD5(upToken.getPassword(),sysUser.getSalt()));
         ((PasswordToken) authenticationToken).setPassword("e0b141de1c8091be350d3fc80de66528");
         return new SimpleAuthenticationInfo(username,sysUser.getPassword(),getName());
     } else {
         return new SimpleAuthenticationInfo(username,"",getName());
     }
  }
public void setUserService(SysUserService userService) {
    this.userService = userService;
}
  
}
